package com.techstar.common.jackson;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.techstar.api.pojo.bus.TodayHourTrafficTrend;

public class TodayHourTrafficTrendDeserializer extends StdDeserializer<String>{
		private static final long serialVersionUID = 1L;

		public TodayHourTrafficTrendDeserializer() {
	        this(String.class);
	    }

	    protected TodayHourTrafficTrendDeserializer(Class<?> type) {
	        super(type);
	    }
		
		@Override
		public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException{
			TodayHourTrafficTrend d = (TodayHourTrafficTrend)ctxt.getParser().getParsingContext().getCurrentValue();
			if (StringUtils.isBlank(jp.getText())||"null".equals(jp.getText().toLowerCase())) {
				return (String) ctxt.handleUnexpectedToken(this.handledType(), jp.getCurrentToken(), jp, "expected String, Number or JSON Array", new Object[0]);
			}else {
				DateTime createTime = DateTime.parse(jp.getText().trim(), DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
				d.setExecHour(createTime.getHourOfDay()+"");
				return createTime.toString("yyyy-MM-dd HH");
			}
		}

	}